/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.wcalendar.business.security.boundary;

import it.polimi.wcalendar.business.security.entity.District;
import java.security.Principal;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author baldo
 */
public class DistrictManager {
    
    /*@Inject
    Principal principal;
    
    @PersistenceContext
    EntityManager em;*/
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("authPU");
    @PersistenceContext
    EntityManager em = emf.createEntityManager();
        
    public List getAll(){
        Query createNamedQuery = em.createNamedQuery("District.findAll");
        List list = createNamedQuery.getResultList();
        return list;
    }

    public List getByName(){
        Query createNamedQuery;
        createNamedQuery = em.createNamedQuery("District.findByName");
        List list = createNamedQuery.getResultList();
        return list;
    }

    public District loadDistrict(String placeName) {
        return (District) em.createQuery("SELECT d FROM District d WHERE d.name = :name")
                .setParameter("name", placeName)
                .getResultList().get(0);
    }
    
    public boolean save(District district) {
        em.persist(district);
        return true;
    }

}
